#
# Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#

cmake_minimum_required(VERSION 3.8...3.22)

# Project
project(boost_mysql VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX)

# Library
add_library(boost_mysql INTERFACE)
add_library(Boost::mysql ALIAS boost_mysql)

# Dependencies. If non-Boost dependencies are not found, we just bail out
find_package(Threads)
if(NOT Threads_FOUND)
    message(STATUS "Boost.MySQL has been disabled, because the required package Threads hasn't been found")
    return()
endif()
find_package(OpenSSL)
if(NOT OpenSSL_FOUND)
    message(STATUS "Boost.MySQL has been disabled, because the required package OpenSSL hasn't been found")
    return()
endif()

# This is generated by boostdep.
# Note that Boost::pfr is not listed because it's a peer dependency
target_link_libraries(
    boost_mysql
    INTERFACE
    Boost::asio
    Boost::assert
    Boost::charconv
    Boost::config
    Boost::core
    Boost::describe
    Boost::endian
    Boost::intrusive
    Boost::mp11
    Boost::optional
    Boost::system
    Boost::throw_exception
    Boost::variant2
    Threads::Threads
    OpenSSL::Crypto
    OpenSSL::SSL
)

# Includes & features
target_include_directories(boost_mysql INTERFACE include)
target_compile_features(boost_mysql INTERFACE cxx_std_11)

# Don't run integration testing unless explicitly requested, since these require a running MySQL server
option(BOOST_MYSQL_INTEGRATION_TESTS OFF "Whether to build and run integration tests or not")
mark_as_advanced(BOOST_MYSQL_INTEGRATION_TESTS)

# Examples and tests
if(BUILD_TESTING)
    # Contains some functions to share code between examples and tests
    include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/test_utils.cmake)

    # Custom target tests; required by the Boost superproject
    if(NOT TARGET tests)
        add_custom_target(tests)
    endif()

    # Tests
    add_subdirectory(test)
    
    if (BOOST_MYSQL_INTEGRATION_TESTS)
        # All examples require a real server to run
        add_subdirectory(example)

        # We don't run benchmarks, but we build them to prevent code rotting.
        # Having them under BOOST_MYSQL_INTEGRATION_TESTS prevents them
        # from being built in superproject builds
        add_subdirectory(bench) 
    endif()
endif()
